<?php namespace App\Core\Metadata;

trait Fielder {
    
    public $create = 'all';
    public $update = 'all';
    
    public function getCreateFields(){
        $exp = $this->create == 'all' ? 'except:'.$this->getPrimaryKey() : $this->create;
        return $this->getFieldNames($exp);
    }
    
    public function getUpdateFields(){
        if($this->update == 'all'){
            return $this->getFieldKeysByFillType('input');
        }
        return $this->getFieldNames($this->update);
    }
    
    protected function getFieldNames($exp){
        if(strpos($exp, 'except:') !== 0){
            return explode(',', $exp);
        }
        $excepts = explode(',', substr($exp, 7));
        $fieldNames = array_keys($this->fields);
        foreach($fieldNames as $key => $val){
            if(in_array($val, $excepts)){
                unset($fieldNames[$key]);
            }
        }
        return $fieldNames;
    }
}